package com.originskyseed.platform.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.ParameterConfigureMapper;
import com.originskyseed.platform.user.model.ParameterConfigure;
import com.originskyseed.platform.user.service.IParameterConfigureService;

/**
 * 参数配置Service接口实现类（OSI_PARAMETER_CONFIGURE）
 *
 * @author Tangwanghui
 */
@DubboService
@Component("parameterConfigureService")
public class ParameterConfigureServiceImpl implements IParameterConfigureService {

	@Resource
	private ParameterConfigureMapper parameterConfigureMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.parameterConfigureMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.parameterConfigureMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(ParameterConfigure record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.parameterConfigureMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(ParameterConfigure record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.parameterConfigureMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(ParameterConfigure record) throws Exception {
		record.setUpdateTime(new Date());
		return this.parameterConfigureMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(ParameterConfigure record) throws Exception {
		record.setUpdateTime(new Date());
		return this.parameterConfigureMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public ParameterConfigure selectByPrimaryKey(String id) throws Exception {
		return this.parameterConfigureMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ParameterConfigure> selectList(ParameterConfigure record) throws Exception {
		return this.parameterConfigureMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<ParameterConfigure> selectListByPage(Map<String, Object> params, BasePage<ParameterConfigure> page) throws Exception {
		params.put("page", page);
		List<ParameterConfigure> list = this.parameterConfigureMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询参数配置树
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public JSONArray selectParameterConfigureTree() throws Exception {
		// 获取所有参数配置列表
		ParameterConfigure deptQuery = new ParameterConfigure();
		List<ParameterConfigure> parameterConfigureList = this.parameterConfigureMapper.selectList(deptQuery);
		List<ParameterConfigure> parentList = new ArrayList<ParameterConfigure>();
		for (ParameterConfigure parameterConfigure : parameterConfigureList) {
			if (StringUtils.isBlank(parameterConfigure.getParentId())) {
				parentList.add(parameterConfigure);
			}
		}
		// 递归构建
		return build2Tree(parameterConfigureList, parentList);
	}

	/**
	 * 查询code是否存在
	 *
	 * @param parameterConfigure
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean existCode(ParameterConfigure parameterConfigure) throws Exception {
		return this.parameterConfigureMapper.existCode(parameterConfigure);
	}

	/**
	 * 查询记录
	 *
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public ParameterConfigure selectByCode(String code) throws Exception {
		return this.parameterConfigureMapper.selectByCode(code);
	}

	/**
	 * 查询记录
	 *
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public ParameterConfigure selectByCode(String tenantCode,String code) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.parameterConfigureMapper.selectByCode(code);
	}

	/**
	 * 查询记录
	 *
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public List<ParameterConfigure> selectByParentCode(String code) throws Exception {
		List<ParameterConfigure> parameterConfigureList = new ArrayList<>();
		ParameterConfigure parameterConfigure = this.parameterConfigureMapper.selectByCode(code);
		if (parameterConfigure != null) {
			ParameterConfigure query = new ParameterConfigure();
			query.setParentId(parameterConfigure.getId());
			parameterConfigureList = this.parameterConfigureMapper.selectList(query);
		}
		return parameterConfigureList;
	}

	/**
	 * 查询所有的参数
	 * 
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ParameterConfigure> selectListByParentId(String parentId) throws Exception {
		return this.parameterConfigureMapper.selectListByParentId(parentId);
	}


	/**
	 * 查询所有参数
	 *
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	public List<ParameterConfigure> selectListByParentId(String tenantCode,String parentId) throws Exception
	{
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return selectListByParentId(parentId);
	}

	/**
	 * 构建树
	 *
	 * @param list       包含所有数据
	 * @param parentList 只包含根节点或父节点数据
	 * @return
	 */
	private JSONArray build2Tree(List<ParameterConfigure> list, List<ParameterConfigure> parentList) {
		JSONArray ary = new JSONArray();
		if (!list.isEmpty() && !parentList.isEmpty()) {
			JSONObject obj;
			// 移除父节点
			list.removeAll(parentList);
			List<ParameterConfigure> childList;
			for (ParameterConfigure parameterConfigure : parentList) {
				obj = new JSONObject();
				obj.put("id", parameterConfigure.getId());
				obj.put("parentId", parameterConfigure.getParentId());
				obj.put("name", parameterConfigure.getName());
				// 查找子节点
				childList = findChildren(list, parameterConfigure);
				if (hasChildren(childList)) {
					obj.put("children", build2Tree(list, childList));
				}
				ary.add(obj);
			}
		}
		return ary;
	}

	/**
	 * 查找当前父节点下的子节点数据
	 *
	 * @param list       去除父节点的所有数据
	 * @param parentDept 父节点
	 * @return
	 */
	private List<ParameterConfigure> findChildren(List<ParameterConfigure> list, ParameterConfigure parentDept) {
		List<ParameterConfigure> childList = new ArrayList<ParameterConfigure>();
		if (!list.isEmpty()) {
			for (ParameterConfigure parameterConfigure : list) {
				if (parameterConfigure.getParentId().equals(parentDept.getId())) {
					childList.add(parameterConfigure);
				}
			}
		}
		return childList;
	}

	private boolean hasChildren(List<ParameterConfigure> list) {
		return list.size() > 0 ? true : false;
	}
}
